<?php
namespace app\api\model;

use think\Model;
use think\Db;
class UserModel extends Model
{
    function  login($username, $password) {
        $result = Db::name('user')->where('username', $username)->where('password', md5($password))->find();
        return $result;
    }
    function  register($username,$password,$phone,$gender){
          if ($gender=='男'){
              $gender1=1;
          }
          else
          {
              $gender1=0;
          }
          $data=[
              'username'=> $username,
              'password'=>md5($password),
              'picture'=> null,
              'email' =>null,
              'phone' =>$phone,
              'gender'=>$gender1,
              'createtime'=>date("YmdHis"),
              'sport'=>0,
              'state'=>1

          ];
        $num=Db::name("user")->where("username='$username'")->count();
        if ($num) {
            # code...
           return 1;
        }
        else
        {
            $res=Db::name("user")->insert($data);
            if($res){

                return 2;

            }else{
                return 3;
            }
        }

    }
    function issuepicture($userid,$picture1) {
        $data = ['userid'=>$userid, 'picture1' => $picture1];
        $result = Db::name('picture')->insert($data);
        if ($result) {
            return ['success'=>1];
        }else{
            return ['success'=>0];
        }
    }
    function getpicture($userid) {
        $result =  Db::name('picture')
            ->where("userid",$userid)
            ->select();
        return $result;
    }
    function gameenroll($userid, $gameid) {
        $data=[
            'userid'=>$userid,
            'gameid'=>$gameid,
        ];
        $result=Db::name('game_enroll')->insert($data);
        if($result){
            return  ['success'=>1];

        }else{
            return  ['success'=>0];
        }
    }
}